﻿var post_vm = avalon.define({
    $id: 'main',
    posts: [],
    cache_posts: [],
    pages: 0,
    page_size: 12,
    page_index: 0,
    get_posts: function () {
        post_vm.page_index++;
        $.post('/display/get', { pageIndex: post_vm.page_index }).success(post_vm.on_get_posts_success);
    },
    on_get_posts_success: function (datas) {
        post_vm.cache_posts = post_vm.posts;
        for (var i = 0, len = datas.Datas.length; i < len; i++) {
            var data = datas.Datas[i];
            post_vm.cache_posts.push(data);
        }

        post_vm.posts = post_vm.cache_posts;
        post_vm.pages = datas.PageCount; //得到总页数
    },
    get_display_info: function () {
        $.get('/display/getdisplayinfo').success(post_vm.on_get_display_info_success);
    },
    hot_posts: [],
    top_posts: [],
    recommended_posts:[],
    on_get_display_info_success: function (datas) {
        post_vm.hot_posts = datas.HotPosts;
        post_vm.top_posts = datas.TopPosts;
        post_vm.recommended_posts = datas.RecommendedPosts;
    }
});

avalon.filters.not_html = function (str) {
    str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
    str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
    str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
    str = str.replace(/&nbsp;/ig, '').substring(0, 120);//去掉&nbsp;
    return str;
}

post_vm.get_posts();
post_vm.get_display_info();